$(function () {
	// 获取统计数据
	$.ajax({
		timeout: 30000,
		url: window.Http + "/property/count/user/data",
		type: 'GET',
		headers: {
			'Authorization': "Bearer " + sessionStorage.getItem('Token')
		},
		success: function (res) {
			if (res.code == 200) {
				$('.buyProductNumber').html(res.data.productNum);
				$('.registerProductNumber').html(res.data.productSize+ ' GB');
				$('.registerSourceNumber').html(res.data.governNum);
				$('.waitAudit').html(res.data.waitAudit);
				$('.buyUserNumber').html(res.data.governSize + ' GB');
				$('.expendBalance').html(res.data.expendBalance.toFixed(2));
				$('.balance').html(res.data.balance.toFixed(2));
			}
		}
	});

	// 获取折线图数据
	function getEchartsLine(year) {
		$.ajax({
			timeout: 30000,
			url: window.Http + "/count/personal/line",
			type: 'GET',
			headers: {
				'Authorization': "Bearer " + sessionStorage.getItem('Token')
			},
			data: { year: year },
			success: function (res) {
				if (res.code == 200) {
					let timeData = Object.keys(res.data);
					let value = Object.values(res.data);
					let use = [];
					let govern = [];
					value.forEach(item => {
						use.push(item.use);
						govern.push(item.govern);
					});
					let data = {
						timeData,
						use,
						govern
					};
					lineChart(data);
				}
			}
		});
	}

	// 绘制折线图
	function lineChart(data) {
		$("#main").css("width", ($(".container").width() - 200));
		var myChart = echarts.init(document.getElementById('main'));
		// 指定图表的配置项和数据
		var option = {
			tooltip: {
				trigger: 'axis',
				axisPointer: { type: 'cross' }
			},
			legend: {
				data: ['数据产品量', '已治理数据量'],
				right: '1%'
			},
			grid: {
				left: '3%',
				right: '4%',
				bottom: '3%',
				containLabel: true
			},
			xAxis: {
				type: 'category',
				boundaryGap: false,
				axisTick: {
					alignWithLabel: true
				},
				data: data.timeData
			},
			yAxis: [
				{
					type: 'value',
					name: '数量（份）',
					axisLabel: {
						formatter: '{value}'
					}
				}
			],
			series: [
				{
					name: '数据产品量',
					type: 'line',
					smooth: true,
					data: data.use
				},
				{
					name: '已治理数据量',
					type: 'line',
					smooth: true,
					data: data.govern
				},
			]
		};
		// 使用刚指定的配置项和数据显示图表。
		myChart.setOption(option);
	}

	// 获取年份
	function getYear() {
		var date = new Date();
		var year = date.getFullYear();
		let html = '';
		$('#inputState').empty();
		for (var i = year; i >= 2020; i--) {
			html += `<option value="${i}">${i}年</option>`;
		}
		$('#inputState').append(html);
		$('#inputState').val(year + '年'); // 默认选中当前年份
	}

	getYear();
	var currentYear = new Date().getFullYear();
	getEchartsLine(currentYear);

	$("#inputState").change(function () {
		let year = $(this).val().substring(0, $(this).val().length - 1);
		getEchartsLine(year);
	});
});
